//
//  String+Ex.swift
//  Pods
//
//  Created by Rdxer on 2017/7/18.
//
//

import Foundation


public extension String{
    /// to URL
    public var toUrl: URL? {
        if let url = URL.init(string:self){
            return url
        }
        if #available(iOS 16.0, *) {
            return URL.init(filePath: self)
        } else {
            return URL.init(fileURLWithPath: self)
        }
    }
}

public extension String{
    /// 去空格 & 换行符
    var trim: String {
        return self.trimmingCharacters(in: .whitespacesAndNewlines)
    }
    
  
    mutating func xx_trim(str:String) ->String {
        self = xx_trimPrefix(str: str)
        self = xx_trimSuffix(str: str)
        return self
    }
    
    mutating func xx_trimPrefix(str:String) ->String {
        if str.length == 0 {
            return self
        }
        
        while self.hasPrefix(str) {
            self = self.asNSString.substring(from: str.length)
        }
        
        return self
    }
    
 
    mutating func xx_trimSuffix(str:String) ->String {
        if str.length == 0 {
            return self
        }
        
        while self.hasSuffix(str) {
            self = self.asNSString.substring(to: length - str.length)
        }
        
        return self
    }
    
}

